/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import com.google.common.collect.ImmutableMap;

import java.util.*;
import java.util.Map.Entry;

import static commons.util.ExceptionUtil.throwArgEx;

/**
 * @author Susanta Tewari
 * @version 1.0 Dec 3, 2010
 */
public class MapUtil {

    private MapUtil() {}

    /**
     * sorting a map based on value
     *
     * @param map map to be sorted
     * @param comparator to be used in sorting
     * @return a new instance of map with its keys sorted
     */
    public static Map sortByValue(Map map, Comparator comparator) {

        List<Map.Entry> list          = new ArrayList<Map.Entry>(map.entrySet());
        Comparator mapEntryComparator = new MapEntryComparator(comparator);

        Collections.sort(list, mapEntryComparator);


        // load into new map
        Map result = new LinkedHashMap();

        for (Map.Entry entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;
    }

    /**
     * @param keys key array
     * @param vals value array
     * @param <K> key type
     * @param <V> value type
     * @return an immutable map with the specified keys and values
     */
    public static <K, V> ImmutableMap<K, V> createMap(K[] keys, V[] vals) {

        Objects.requireNonNull(keys);
        Objects.requireNonNull(vals);
        throwArgEx(keys.length != vals.length, "length mismatch");

        Map<K, V> map = new HashMap<>(keys.length);

        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i], vals[i]);
        }

        return ImmutableMap.copyOf(map);
    }

    /**
     * @since 1.0.0
     * @author Susanta Tewari
     */
    private static class MapEntryComparator<V> implements Comparator<Map.Entry<?, V>> {

        @SuppressWarnings("PackageVisibleField")
        Comparator<V> comparator;

        public MapEntryComparator() {

            comparator = new Comparator<V>() {

                @Override
                public int compare(V o1, V o2) {
                    return ((Comparable) o1).compareTo(o2);
                }
            };
        }

        public MapEntryComparator(Comparator<V> comparator) {
            this.comparator = comparator;
        }

        @Override
        public int compare(Entry<?, V> o1, Entry<?, V> o2) {
            return comparator.compare(o1.getValue(), o2.getValue());
        }
    }
}
